#!/usr/bin/python
# coding:utf-8
from urllib import request
from bs4 import BeautifulSoup
import threading, time
charset = 'gbk'
# User_Agent信息
user_agent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.67 Safari/537.36"
# headers信息
head = {'User-Agent': user_agent, 'Connection': "keep-alive"}


def chapter_index_url(url):
    # 创建Requset对象
    req = request.Request(url=url, headers=head)
    html = request.urlopen(req).read().decode(charset)
    # 解析网站
    soup = BeautifulSoup(html, 'lxml')

    chapter_index_url = []
    begin_flag = False
    for child in soup.dl.children:
        if child != '\n':
            if child.string == u'《龙王传说》正文卷':
                begin_flag = True
            if begin_flag and child.a != None:
                chapter_index_url.append("https://www.biqukan.com" + child.a.get('href'))
                pass
            pass
        pass
    return chapter_index_url
    pass


def chapter(index_url, num):
    req = request.Request(url=index_url, headers=head)
    html = request.urlopen(req).read().decode(charset)
    soup = BeautifulSoup(html, 'lxml')
    title = soup.h1.string
    text = soup.find_all(id='content', class_="showtxt")
    text = str(text).replace('\r', '\r\n')
    text = text.replace('\xa0', '')
    text = text.replace('<br/>', '')
    text = text.replace('<div class="showtxt" id="content">', '')
    txt = title + "\n\n" + text

    with open('龙王传说//' + str(num) + '.txt', 'w') as file:
        print(txt, file=file)
    # print(txt)
    pass


cnt = 0


def from_to(index, f, t):
    for i in range(int(f), int(t)):
        global cnt
        cnt += 1
        chapter(index[i], i)
    pass


def jindu(len):
    global cnt
    while True:
        time.sleep(0.2)
        print("\r{}/{}   {:.2f}%".format(cnt, len, (cnt / len) * 100), end="")
        pass


if __name__ == "__main__":
    url = "https://www.biqukan.com/1_1496/"
    index = chapter_index_url(url)
    len = len(index)
    t3 = threading.Thread(target=jindu, args=(len,), daemon=True)
    t1 = threading.Thread(target=from_to, args=(index, 0, len / 2))
    t2 = threading.Thread(target=from_to, args=(index, len / 2, len))
    t3.start()
    t1.start()
    t2.start()
    t2.join()
